package com.karaku.karaku_collector.control;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.karaku.karaku_collector.util.UIUtil;

/**
 * Created by guoping on 2017/11/7.
 */

public class PreviewMaskView extends View {

    private final int MAX_BORDER_VERTICAL = UIUtil.dp2px(180);
    private final int MAX_BORDER = UIUtil.dp2px(120);

    private Paint mDstPaint, mSrcPaint;

    private float verticalRation, horizontalRatio;

    public PreviewMaskView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        mDstPaint = new Paint();
        mSrcPaint = new Paint();
        mDstPaint.setAntiAlias(true);
        mSrcPaint.setAntiAlias(true);
        mDstPaint.setColor(Color.RED);
        mSrcPaint.setColor(Color.BLUE);
    }

    public void setVerticalRatio(float verticalRatio) {
        this.verticalRation = verticalRatio;
        invalidate();
    }

    public void setHorizontalRatio(float horizontalRatio) {
        this.horizontalRatio = horizontalRatio;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int dp5 = UIUtil.dp2px(5);
        int dp40 = dp5 * 8;
        int dp37 = UIUtil.dp2px(37);
        int borderX = (int) (MAX_BORDER * horizontalRatio);
        int borderY = (int) (MAX_BORDER_VERTICAL * verticalRation);

        Bitmap bmp = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
//        c.drawColor(0XFFE51E58);
        c.drawColor(0XFF000000);

        mSrcPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        RectF rect = new RectF(borderX, borderY, getWidth() - borderX, getHeight() - borderY);
        c.drawRoundRect(rect, dp40, dp40, mSrcPaint);

//        c.drawColor(0X88E51E58);
        c.drawColor(0X88000000);

        mSrcPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        rect = new RectF(borderX + dp5, borderY + dp5, getWidth() - (borderX + dp5), getHeight() - (borderY + dp5));
        c.drawRoundRect(rect, dp37, dp37, mSrcPaint);

        canvas.drawBitmap(bmp, 0, 0, null);
    }
}
